﻿// (c) 版权所有 TriangleGames 2012-2012

using UnityEditor;
using UnityEngine;

namespace TriangleGames.TrainResEditor
{
    [CustomEditor(typeof(TrainResGUICore))]
    public class TrainResGUIInspectorCore : Editor
    {
        private TrainResGUICore guiComponent;

        void OnEnable()
        {
            guiComponent = target as TrainResGUICore;

            CheckForDuplicateComponents();
        }

        public override void OnInspectorGUI()
        {
            EditorGUIUtility.LookLikeInspector();

            GUILayout.Label("注意", EditorStyles.boldLabel);

            GUILayout.Label("- 一个场景应该只有一个TrainResGUI组件.\n- 资源编辑器 将会自动添加此组件.\n- 可以在设置中禁止自动添加.");

            GUILayout.Label("通用选项", EditorStyles.boldLabel);

            EditorGUI.indentLevel = 1;

            guiComponent.enableGUILayout = EditorGUILayout.Toggle(new GUIContent("激活 GUILayout",
                                                                           "禁用 GUILayout 能提高GUI动作的性能, 特别是在移动设备上. 注意: 你不能使用GUILayout动作当GUILayout被禁用时."),
                                                                           guiComponent.enableGUILayout);
            guiComponent.controlMouseCursor = EditorGUILayout.Toggle(new GUIContent("控制鼠标光标",
                                                                              "如果你有脚本需要控制鼠标光标时可以禁用此选项."),
                                                                              guiComponent.controlMouseCursor);

            guiComponent.previewOnGUI = EditorGUILayout.Toggle(new GUIContent("编辑的时候预览GUI动作", "这个选项可以让你在编辑的时候预览GUI动作. 注意: 目前功能还不是很稳定!"), guiComponent.previewOnGUI);

            EditorGUI.indentLevel = 0;
            GUILayout.Label("调试选项", EditorStyles.boldLabel);
            EditorGUI.indentLevel = 1;

            guiComponent.drawStateLabels = EditorGUILayout.Toggle(new GUIContent("显示激活状态标签", "在游戏视图中显示当前激活状态. 你可以在观察器为每个状态机激活/禁用此选项."), guiComponent.drawStateLabels);

            GUI.enabled = guiComponent.drawStateLabels;
            //EditorGUI.indentLevel = 2;

            guiComponent.GUITextureStateLabels = EditorGUILayout.Toggle(new GUIContent("GUITexture状态标签", "在GUITextures上显示激活状态标签."), guiComponent.GUITextureStateLabels);
            guiComponent.GUITextStateLabels = EditorGUILayout.Toggle(new GUIContent("GUIText状态标签", "在GUITexts上显示激活状态标签."), guiComponent.GUITextStateLabels);

            GUI.enabled = true;
            //EditorGUI.indentLevel = 1;

            guiComponent.filterLabelsWithDistance = EditorGUILayout.Toggle(new GUIContent("根据距离过滤状态标签", "当你在游戏视图中移动并想看到距离最近的状态标签时此选项是非常有用的."), guiComponent.filterLabelsWithDistance);

            GUI.enabled = guiComponent.filterLabelsWithDistance;

            guiComponent.maxLabelDistance = EditorGUILayout.FloatField(new GUIContent("距离", "从主镜头开始算起的距离"), guiComponent.maxLabelDistance);

            if (GUI.changed)
            {
                CheckForDuplicateComponents();
            }
        }

        void CheckForDuplicateComponents()
        {
            var components = FindObjectsOfType(typeof(TrainResGUICore));

            if (components.Length > 1)
            {
                if (EditorUtility.DisplayDialog("状态机", "场景中有不止一个TrainResGUI!\n是否移除其他实例?", "是", "否"))
                {
                    foreach (Object component in components)
                    {
                        if (component != target)
                        {
                            var behavior = (TrainResGUICore)component as Behaviour;

                            if (behavior.gameObject.GetComponents(typeof(Component)).Length == 2) // every game object has a transform component
                            {
                                if (EditorUtility.DisplayDialog("状态机", "删除: " + behavior.gameObject.name + "?", "是", "否"))
                                {
                                    DestroyImmediate(behavior.gameObject);
                                }
                            }

                            DestroyImmediate(component);

                        }
                    }
                }
            }
        }

    }
}